//
//  LoanUnitView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/7.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanUnitViewDelegate {
    @objc optional func getUnitText(currentView:LoanUnitView) //获取文本内容
}

class LoanUnitView: UIView {
    
    weak var delegate : LoanUnitViewDelegate?
    
    fileprivate let titleLabel = UILabel() //标题
    fileprivate let textView = UIView()
    let textLabel = UITextField.init(Width: 5) //内容
    fileprivate let unitLabel = UILabel() //单位
    
    var title : String! {//标题
        didSet{
            titleLabel.text = title
        }
    }
    
    var text : String! {//内容
        didSet{
            textLabel.text = text
        }
    }
    
    var placeholder : String! {//提示内容
        didSet{
            textLabel.placeholder = placeholder
        }
    }
    
    var unitText : String! {
        didSet{
            unitLabel.text = unitText
            unitLabel.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(RATIO(maxNum: -5))
                make.centerY.height.equalToSuperview()
                make.width.equalTo(unitText.size(font: RATIO(maxNum: 16), height: 35).width-8)
            }
        }
    }

    init(textWidth:CGFloat?=ScreenW/3*2) {
        super.init(frame: .zero)
        
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleLabel.textAlignment = .right
        self.addSubview(titleLabel)
        
        textView.layer.borderColor = UIColor.darkGray.cgColor
        textView.layer.borderWidth = 1
        self.addSubview(textView)
        
        textLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        textLabel.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        textView.addSubview(textLabel)
        
        unitLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        unitLabel.textAlignment = .center
        textView.addSubview(unitLabel)
        
        textView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalToSuperview()
            make.width.equalTo(textWidth!)
            make.height.equalTo(35)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.right.equalTo(textView.snp.left).offset(RATIO(maxNum: -5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            
        }
        
        unitLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.height.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.left.height.centerY.equalToSuperview()
            make.right.equalTo(unitLabel.snp.left)
        }
    }
    
    //MARK: 监听文输入本框内容
    @objc fileprivate func textFieldDidChange() {
        
        text = textLabel.text
        self.delegate?.getUnitText!(currentView: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
